package com.demo.expandablelistviewdemo;

import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    private String groups[] = {"高中同学", "大学同学"};
    private String names[][] = {{"虎头", "弄玉"}, {"清照", "李白"}};
    private int imgIds[][] = {{R.drawable.tiger, R.drawable.nongyu}, {R.drawable.qingzhao, R.drawable.libai}};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ExpandableListView expList = findViewById(R.id.expand_list);

        BaseExpandableListAdapter adapter = new BaseExpandableListAdapter() {
            @Override
            public int getGroupCount() {
                return groups.length;
            }

            @Override
            public int getChildrenCount(int i) {
                return names[i].length;
            }

            @Override
            public Object getGroup(int i) {
                return null;
            }

            @Override
            public Object getChild(int i, int i1) {
                return null;
            }

            @Override
            public long getGroupId(int i) {
                return 0;
            }

            @Override
            public long getChildId(int i, int i1) {
                return 0;
            }

            @Override
            public boolean hasStableIds() {
                return false;
            }

            @Override
            public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {
                TextView txtView = new TextView(MainActivity.this);
                txtView.setText(groups[i]);
                txtView.setTextSize(25);
                txtView.setPadding(70, 10, 0, 10);
                return txtView;
            }

            @Override
            public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
                LinearLayout llayout = new LinearLayout(MainActivity.this);
                llayout.setOrientation(LinearLayout.HORIZONTAL);
                llayout.setPadding(40, 16, 16, 16);
                llayout.setGravity(Gravity.CENTER_VERTICAL);
                ImageView img = new ImageView(MainActivity.this);
                img.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
                img.setScaleType(ImageView.ScaleType.FIT_CENTER);
                img.setImageResource(imgIds[groupPosition][childPosition]);
                llayout.addView(img);

                TextView txt = new TextView(MainActivity.this);
                txt.setPadding(20, 0, 0, 0);
                txt.setText(names[groupPosition][childPosition]);
                txt.setTextSize(20);
                llayout.addView(txt);
                return llayout;
            }

            @Override
            public boolean isChildSelectable(int i, int i1) {
                return true;
            }
        };
        expList.setAdapter(adapter);

//        ExpandableListAdapter expAdapter=new ExpandableListAdapter() {
//            @Override
//            public int getGroupCount() {
//                return groups.length;
//            }
//
//            @Override
//            public View getGroupView(int groupPosition, boolean b, View view, ViewGroup viewGroup) {
//                TextView txtView=new TextView(MainActivity.this);
//                txtView.setText(groups[groupPosition]);
//                txtView.setTextSize(25);
//                txtView.setPadding(70,10,0,10);
//                return txtView;
//            }
//
//            @Override
//            public int getChildrenCount(int groupPosition) {
//                return imgIds[groupPosition].length;
//            }
//
//            @Override
//            public View getChildView(int groupPosition, int childPosition, boolean b, View view, ViewGroup viewGroup) {
//                LinearLayout llayout=new LinearLayout(MainActivity.this);
//                llayout.setOrientation(LinearLayout.HORIZONTAL);
//                llayout.setPadding(40, 16, 16, 16);
//                llayout.setGravity(Gravity.CENTER_VERTICAL);
//                ImageView img=new ImageView(MainActivity.this);
//                img.setLayoutParams(new ViewGroup.LayoutParams(100, 100));
//                img.setScaleType(ImageView.ScaleType.FIT_CENTER);
//                img.setImageResource(imgIds[groupPosition][childPosition]);
//                llayout.addView(img);
//
//                TextView txt=new TextView(MainActivity.this);
//                txt.setPadding(20,0,0,0);
//                txt.setText(names[groupPosition][childPosition]);
//                txt.setTextSize(20);
//                llayout.addView(txt);
//                return llayout;
//            }
//
//            @Override
//            public void registerDataSetObserver(DataSetObserver dataSetObserver) {
//
//            }
//
//            @Override
//            public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {
//
//            }
//
//            @Override
//            public Object getGroup(int i) {
//                return null;
//            }
//
//            @Override
//            public Object getChild(int i, int i1) {
//                return null;
//            }
//
//            @Override
//            public long getGroupId(int i) {
//                return 0;
//            }
//
//            @Override
//            public long getChildId(int i, int i1) {
//                return 0;
//            }
//
//            @Override
//            public boolean hasStableIds() {
//                return false;
//            }
//
//            @Override
//            public boolean isChildSelectable(int i, int i1) {
//                return false;
//            }
//
//            @Override
//            public boolean areAllItemsEnabled() {
//                return false;
//            }
//
//            @Override
//            public boolean isEmpty() {
//                return false;
//            }
//
//            @Override
//            public void onGroupExpanded(int i) {
//
//            }
//
//            @Override
//            public void onGroupCollapsed(int i) {
//
//            }
//
//            @Override
//            public long getCombinedChildId(long l, long l1) {
//                return 0;
//            }
//
//            @Override
//            public long getCombinedGroupId(long l) {
//                return 0;
//            }
//        };
//        expList.setAdapter(expAdapter);
        expList.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView expandableListView, View view, int groupPosition, int childPosition, long l) {
                Toast.makeText(MainActivity.this, "你要与" + groups[groupPosition] + names[groupPosition][childPosition] + "聊天", Toast.LENGTH_LONG).show();
                return false;
            }
        });
    }
}
